public class test02 {
    class Solution {
        public int longestCommonSubsequence(String text1, String text2) {
            char[] ch1 = text1.toCharArray();
            char[] ch2 = text2.toCharArray();
            int[][] dp = new int[ch1.length+1][ch2.length+1];
            int res = 0;

            for(int i = 1;i<ch1.length+1;i++){
                for(int j = 1;j<ch2.length+1;j++){
                    if(ch1[i-1]==ch2[j-1]){
                        dp[i][j] = dp[i-1][j-1]+1;
                    }else{
                        //不要求连续，所以不相等时也要更新dp[i][j]
                        dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
                    }
                    res = Math.max(res,dp[i][j]);
                }
            }
            return res;
        }
    }
}
